package org.darkforest.common.services.impl;

import org.darkforest.common.services.GoodsClassRedisService;
import org.darkforest.common.services.GoodsRedisService;
import org.darkforest.dao.mapper.GoodsMapper;
import org.darkforest.dao.redis.GoodsRedisDao;
import org.darkforest.model.Goods;
import org.darkforest.model.GoodsClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by valentina on 15/5/13.
 */
@Service
public class GoosRedisServiceImpl implements GoodsRedisService{
    Logger logger = LoggerFactory.getLogger(GoosRedisServiceImpl.class);



    @Autowired
    private GoodsRedisDao goodsRedisDao;                //常用商品存入redis中

    @Autowired
    private GoodsClassRedisService goodsClassRedisService;      //获取商品分类

    @Autowired
    private GoodsMapper goodsMapper;


    /**
     * 初始化首页商品信息，项目启动时自动运行
     */
    @Override
    @PostConstruct
    public void initMainGoods() {
        logger.debug("======begin initial goods list for index page======");
        //获取推荐等商品列表，写入redis
        Goods param = new Goods();
//        param.setGoodsCommend((byte)1);
//        param.setGoodsState((byte)1);
//        param.setGoodsVerify((byte)1);
//        param.setGoodsType(0);
        param.setLimit(maxNum);
//        param.setOrderBy("goods_addtime");
        //推荐商品
        param.setRecommondType(2);
        List<Goods> commendList = goodsMapper.findBoothGoodsByCond(param);
        //热销商品
//        param.setGoodsCommend(null);
//        param.setOrderBy("goods_salenum");
        param.setRecommondType(3);
        List<Goods> hotList = goodsMapper.findBoothGoodsByCond(param);
        //最新商品
//        param.setOrderBy("goods_addtime");
        param.setRecommondType(1);
        List<Goods> newList = goodsMapper.findBoothGoodsByCond(param);
        //存入redis
        goodsRedisDao.addGoodsList(COMMEND_GOODS,commendList);
        goodsRedisDao.addGoodsList(HOT_GOODS,hotList);
        goodsRedisDao.addGoodsList(NEW_GOODS,newList);

        //获取各个分类下商品列表，写入redis
        List<GoodsClass> clsList = goodsClassRedisService.getGoodsClassList(null);
        for(GoodsClass cls:clsList){
            param.setGcId1(cls.getGcId());
//            param.setGoodsState((byte)1);
//            param.setGoodsVerify((byte)1);
//            param.setGoodsType(0);
            List<Goods> goodsList = goodsMapper.findRecommendGoodsByCond(param);
            //写入redis
            goodsRedisDao.addGoodsList("goods_list_"+cls.getGcId(),goodsList);
        }
    }

    @Override
    public List<List<Goods>> getGoodsListByCls() {
        List<List<Goods>> res = new ArrayList<List<Goods>>();
        List<GoodsClass> clsList = goodsClassRedisService.getGoodsClassList(null);
        for(GoodsClass cls:clsList){
            res.add(goodsRedisDao.getGoodsListByKey("goods_list_"+cls.getGcId()));
        }
        return res;
    }

    private List<Goods> getGoodsList(Goods param) {
        return goodsMapper.findSimpleGoodsByCond(param);
    }

    /**
     * 从redis中获取推荐商品列表
     * @return
     */
    @Override
    public List<Goods> getHomePageGoodsList(Integer num,String listType){
        Goods param = new Goods();
//        param.setGoodsTypesState((byte)1);
        param.setGoodsState((byte)1);
        param.setGoodsVerify((byte)1);
        param.setGoodsType(0);
        param.setLimit(8);
        if(listType.equals(GoodsRedisService.COMMEND_GOODS)){
//            param.setGoodsCommend((byte)1);
//            param.setOrderBy("goods_addtime");
            param.setRecommondType(2);
        }
        if(listType.equals(GoodsRedisService.HOT_GOODS)){
            param.setRecommondType(3);
//            param.setOrderBy("goods_salenum");
        }
        if(listType.equals(GoodsRedisService.NEW_GOODS)){
            param.setRecommondType(1);
//            param.setOrderBy("goods_addtime");
        }
        List<Goods> res = goodsRedisDao.getGoodsListByKey(listType);
        if(num!=null && res!=null && num<res.size()){
            res = res.subList(0,num-1);
        }
        if(res == null || res.size()==0){
//            res = goodsMapper.findSimpleGoodsByCond(param);
            res = goodsMapper.findBoothGoodsByCond(param);
        }
        return res;
    }

    @Override
    public int getGoodsListSize(String key) {
        List<Goods> list = goodsRedisDao.getGoodsListByKey(key);
        return list==null?0:list.size();
    }
}
